var LAYUI_layedit;
var layedit;

/**
 * 初始化一个富文本编辑器
 * @param id
 * @param layedit 载入的 layedit
 *
 *
 * ifr.layui.layedit.getContent(1);
 * 回调拿值的写法
 */
function layedit_initialize(layedit,id) {
    //构建一个默认的编辑器
    return layedit.build(id, {
        //暴露layupload参数设置接口 --详细查看layupload参数说明
        uploadImage: {
            url: 'common/uploadFile',
            accept: 'image',
            acceptMime: 'image/*',
            exts: 'jpg|png|gif|bmp|jpeg',
            size: 1024 * 10,
            done: function (data) {
            }
        }
        //右键删除图片/视频时的回调参数，post到后台删除服务器文件等操作，
        //传递参数：
        //图片： imgpath --图片路径
        //视频： filepath --视频路径 imgpath --封面路径
        , calldel: {
            url: 'common/delFile',
            done: function (data) {
            }
        }
        //开发者模式 --默认为false
        , devmode: true
        //插入代码设置 --hide:true 等同于不配置codeConfig
        , codeConfig: {
            hide: false,  //是否显示编码语言选择框
            default: 'javascript' //hide为true时的默认语言格式
        }
        , videoAttr: ' preload="none" '
        , tool: [
            'html', 'undo', 'redo', 'code', 'strong', 'italic', 'underline', 'del', 'addhr', '|', 'fontFomatt', 'fontfamily', 'fontSize', 'fontBackColor', 'colorpicker'
            , '|', 'left', 'center', 'right', '|', 'link', 'unlink', 'image_alt', 'anchors'
            , '|'
            , 'table'
            , 'fullScreen'
        ]
        , height: '300px'
    });
}

/**
 * 上传文件
 * @param elem 上传按钮的id
 * @param accept 允许上传的文件类型 可选值有：images（图片）、file（所有文件）、video（视频）、audio（音频）
 * @param acceptMime 规定打开文件选择框时，筛选出的文件类型，值为用逗号隔开的 MIME 类型列表
 acceptMime: 'image/*'（只显示图片文件）
 acceptMime: 'image/jpg, image/png'（只显示 jpg 和 png 文件）
 * @param done  回调方法
 */
function upload_file(elem, accept, acceptMime, done) {
    layui.use(['index', 'form', 'upload'], function () {
        var $ = layui.$;
        var upload = layui.upload;
        upload.render({
            elem: '#' + elem
            , url: 'common/uploadFile'
            , accept: accept
            , method: 'get'
            , acceptMime: acceptMime
            , done: function (res) {
                done(res);
            }
        });
    })
}

/**
 * 打开一个修改/添加页面
 * @param exports 参数集合
 *             title：标题 必填
 *             url ： 网址 必填
 *             maxmin：最大最小化。 可为空 默认不显示  true/false
 *             area : 大小   可为空  格式为 ['800px','600px'] 推荐使用 ['80%','80%']
 *             param： 打开之前的参数  可不填 会自动回调返回
 *             back: 回调方法 必填
 *                          iframeWindow 当前的窗体
 *                          index  layer 关闭当前窗口的标识
 *                          data  回调字段 其中data.field 为回调字段
 *
 * @returns {*}
 */
function editLayer(exports) {
    return topLayer.open({
        type: 2
        , title: exports.title
        , content: exports.url
        , maxmin: exports.maxmin
        , area: exports.area
        , btn: ['确定', '取消']
        , yes: function (index, layero) {
            var iframeWindow = top.window['layui-layer-iframe' + index]
                , submitID = 'submit'
                , submit = layero.find('iframe').contents().find('#' + submitID);
            //监听提交
            iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                if (exports.param == null) {
                    exports.back(iframeWindow, index, data);
                } else {
                    exports.back(iframeWindow, index, data, exports.param);
                }
            });
            submit.trigger('click');
        }
    });
    /*return top.layui.admin.popup({
        type: 2
        , title: exports.title
        , content: exports.url
        , maxmin: exports.maxmin
        , area: exports.area
        , btn: exports.btn ? exports.btn : ['确定', '取消']
        , yes: function (index, layero) {
            var iframeWindow = top.window['layui-layer-iframe' + index]
                , submitID = 'submit'
                , submit = layero.find('iframe').contents().find('#' + submitID);
            //监听提交
            iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                if (exports.param == null) {
                    exports.back(iframeWindow, index, data);
                } else {
                    exports.back(iframeWindow, index, data, exports.param);
                }
            });
            submit.trigger('click');
        }
    });*/
}


function openLayer(exports) {
    return topLayer.open({
        type: 2
        , title: exports.title
        , content: exports.url
        , maxmin: exports.maxmin
        , area: exports.area
        , btn: ['确定', '取消']
        , yes: function (index, layero) {
            var iframeWindow = top.window['layui-layer-iframe' + index]
                , submitID = 'submit'
                , submit = layero.find('iframe').contents().find('#' + submitID);
            //监听提交
            iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                if (exports.param == null) {
                    exports.back(iframeWindow, index, data);
                } else {
                    exports.back(iframeWindow, index, data, exports.param);
                }
            });
            submit.trigger('click');
        }
    });
}

function load_val(fun, name) {
    layui.use(['index', 'layer'], function () {
        var $ = layui.$;
        var layer = layui.layer;
        $.ajax({
            type: "post",
            url: "common/getDicList",
            data: {dicName: name},
            async: false,
            dataType: "json",
            success: function (data) {
                if (data.code == 0) {
                    fun(data.data);
                } else {
                    layer.msg(data.msg);
                }
            }
        });
    });
}

function getDicListName(list, code) {
    var name = code;
    if (list === undefined || list == null) {
        return name;
    }
    for (let i = 0; i < list.length; i++) {
        if (list[i].itemCode == code) {
            name = list[i].itemName;
        }
    }
    return name;
}

//创建监听函数
var xhrOnProgress=function(fun) {
    xhrOnProgress.onprogress = fun; //绑定监听
    //使用闭包实现监听绑
    return function() {
        //通过$.ajaxSettings.xhr();获得XMLHttpRequest对象
        var xhr = $.ajaxSettings.xhr();
        //判断监听函数是否为函数
        if (typeof xhrOnProgress.onprogress !== 'function')
            return xhr;
        //如果有监听函数并且xhr对象支持绑定时就把监听函数绑定上去
        if (xhrOnProgress.onprogress && xhr.upload) {
            xhr.upload.onprogress = xhrOnProgress.onprogress;
        }
        return xhr;
    }
};



/**
 * 功能说明：初始化省市区
 * @author  MrDoubi 2019-05-30 15:49:48
 */
function initPCA(province,city,area) {
    var res = "";
    if(province !== "" && province !== undefined && province != null ){
        for (var pkey in top.provinceMap) {
            if(pkey === province){
                res = top.provinceMap[pkey];
            }
        }
    }
    if(city !== "" && city !== undefined && city != null ){
        for (var ckey in top.cityMap) {
            if(ckey === city){
                res += "-"+top.cityMap[ckey];
            }
        }
    }
    if(area !== "" && area !== undefined && area != null ){
        for (var akey in top.areaMap) {
            if(akey === area){
                res += "-"+top.areaMap[akey];
            }
        }
    }
    return res;
}


/**
 *
 * 功能说明：初始化省市区
 * @author  MrDoubi 2019-05-30 15:49:48
 * @param form layui.form
 * @param province_filter 省份select的filter
 * @param city_filter 市select的filter
 * @param province  省份的select对象
 * @param city  市的select对象
 * @param area  区域的select对象
 */
function initLinkage(form,province_filter,city_filter,province,city,area) {
    layui.$.ajax({
        type: "post",
        url: "common/getProvince",
        data: {},
        async: false,
        dataType: "json",
        success: function (data) {
            if (data.code === 0) {
                var provinceCode = province.attr("code");
                var html = "<option value=\"\">请选择</option>";
                for (let i = 0; i < data.data.length; i++) {
                    if(provinceCode != null && provinceCode !== undefined && provinceCode == data.data[i].provinceCode){
                        html += "<option value=\"" + data.data[i].provinceCode + "\" selected>" + data.data[i].provinceName + "</option>";
                    }else {
                        html += "<option value=\"" + data.data[i].provinceCode + "\">" + data.data[i].provinceName + "</option>";
                    }
                }
                province.html(html);
                if(provinceCode != null && provinceCode !== undefined){
                    layui.$.ajax({
                        type: "post",
                        url: "common/getCity",
                        data: {provinceCode: provinceCode},
                        async: false,
                        dataType: "json",
                        success: function (data) {
                            if (data.code === 0) {
                                var cityCode = city.attr("code");
                                var html = "<option value=\"\">请选择</option>";
                                for (let i = 0; i < data.data.length; i++) {
                                    if(cityCode != null && cityCode !== undefined && cityCode == data.data[i].cityCode) {
                                        html += "<option value=\"" + data.data[i].cityCode + "\" selected>" + data.data[i].cityName + "</option>";
                                    }else {
                                        html += "<option value=\"" + data.data[i].cityCode + "\">" + data.data[i].cityName + "</option>";
                                    }
                                }
                                city.html(html);
                                if(cityCode != null && cityCode !== undefined){
                                    layui.$.ajax({
                                        type: "post",
                                        url: "common/getArea",
                                        data: {cityCode: cityCode},
                                        async: false,
                                        dataType: "json",
                                        success: function (data) {
                                            if (data.code === 0) {
                                                var areaCode = area.attr("code");
                                                var html = "<option value=\"\">请选择</option>";
                                                for (let i = 0; i < data.data.length; i++) {
                                                    if(areaCode != null && areaCode !== undefined && areaCode == data.data[i].areaCode) {
                                                        html += "<option value=\""+data.data[i].areaCode+"\" selected>"+data.data[i].areaName+"</option>";
                                                    }else {
                                                        html += "<option value=\""+data.data[i].areaCode+"\">"+data.data[i].areaName+"</option>";
                                                    }
                                                }
                                                area.html(html);
                                            } else {
                                                layer.msg(data.msg);
                                            }
                                        }
                                    });
                                }
                            } else {
                                layer.msg(data.msg);
                            }
                        }
                    });
                }
            } else {
                layer.msg(data.msg);
            }
        }
    });

    form.render('select');
    form.on('select('+province_filter+')', function(data){
        layui.$(data.elem).trigger('change',data.elem);
        if(data.value !== "") {
            layui.$.ajax({
                type: "post",
                url: "common/getCity",
                data: {provinceCode: data.value},
                async: false,
                dataType: "json",
                success: function (data) {
                    if (data.code === 0) {
                        var html = "<option value=\"\">请选择</option>";
                        for (let i = 0; i < data.data.length; i++) {
                            html += "<option value=\"" + data.data[i].cityCode + "\">" + data.data[i].cityName + "</option>";
                        }
                        city.html(html);
                        area.html("");
                    } else {
                        layer.msg(data.msg);
                    }
                }
            });
        }else {
            city.html("");
            area.html("");
        }
        form.render('select');
    });
    form.on('select('+city_filter+')', function(data){
        layui.$(data.elem).trigger('change',data.elem);
        if(data.value !== ""){
            layui.$.ajax({
                type: "post",
                url: "common/getArea",
                data: {cityCode: data.value},
                async: false,
                dataType: "json",
                success: function (data) {
                    if (data.code === 0) {
                        var html = "<option value=\"\">请选择</option>";
                        for (let i = 0; i < data.data.length; i++) {
                            html += "<option value=\""+data.data[i].areaCode+"\">"+data.data[i].areaName+"</option>";
                        }
                        area.html(html);
                    } else {
                        layer.msg(data.msg);
                    }
                }
            });
        }else {
            area.html("");
        }
        form.render('select');
    });
}



/**
 *
 * 功能说明：初始化省市区
 * @author  MrDoubi 2019-05-30 15:49:48
 * @param form layui.form
 * @param province_filter 省份select的filter
 * @param city_filter 市select的filter
 * @param province  省份的select对象
 * @param city  市的select对象
 */
function initLinka(form,province_filter,city_filter,province,city) {
    layui.$.ajax({
        type: "post",
        url: "common/getProvince",
        data: {},
        async: false,
        dataType: "json",
        success: function (data) {
            if (data.code === 0) {
                var provinceCode = province.attr("code");
                var html = "<option value=\"\">请选择</option>";
                for (let i = 0; i < data.data.length; i++) {
                    if(provinceCode != null && provinceCode !== undefined && provinceCode == data.data[i].provinceCode){
                        html += "<option value=\"" + data.data[i].provinceCode + "\" selected>" + data.data[i].provinceName + "</option>";
                    }else {
                        html += "<option value=\"" + data.data[i].provinceCode + "\">" + data.data[i].provinceName + "</option>";
                    }
                }
                province.html(html);
                if(provinceCode != null && provinceCode !== undefined){
                    layui.$.ajax({
                        type: "post",
                        url: "common/getCity",
                        data: {provinceCode: provinceCode},
                        async: false,
                        dataType: "json",
                        success: function (data) {
                            if (data.code === 0) {
                                var cityCode = city.attr("code");
                                var html = "<option value=\"\">请选择</option>";
                                for (let i = 0; i < data.data.length; i++) {
                                    if(cityCode != null && cityCode !== undefined && cityCode == data.data[i].cityCode) {
                                        html += "<option value=\"" + data.data[i].cityCode + "\" selected>" + data.data[i].cityName + "</option>";
                                    }else {
                                        html += "<option value=\"" + data.data[i].cityCode + "\">" + data.data[i].cityName + "</option>";
                                    }
                                }
                                city.html(html);
                            } else {
                                layer.msg(data.msg);
                            }
                        }
                    });
                }
            } else {
                layer.msg(data.msg);
            }
        }
    });
    form.render('select');
    layui.$(city).trigger('change');
    form.on('select('+province_filter+')', function(data){
        layui.$(data.elem).trigger('change',data.elem);
        if(data.value !== "") {
            layui.$.ajax({
                type: "post",
                url: "common/getCity",
                data: {provinceCode: data.value},
                async: false,
                dataType: "json",
                success: function (data) {
                    if (data.code === 0) {
                        var html = "<option value=\"\">请选择</option>";
                        for (let i = 0; i < data.data.length; i++) {
                            html += "<option value=\"" + data.data[i].cityCode + "\">" + data.data[i].cityName + "</option>";
                        }
                        city.html(html);
                    } else {
                        layer.msg(data.msg);
                    }
                }
            });
        }else {
            city.html("");
        }
        form.render('select');
    });

    form.on('select('+city_filter+')', function(data){
        layui.$(data.elem).trigger('change',data.elem);
    });
}